/*
 *说明：资源信息表 增加/修改脚本
 *
 *@author liuxun 2012-03-18
*/

var opeForm;
var operationType;

$(function(){

	opeForm=$('#opeForm');
	operationType = $('#operationType').val();
	if (operationType == 0) {
		opeForm.url = 'C_RSC!doInsert.do';
	} else if(operationType == 1){
		opeForm.url = 'C_RSC!doUpdate.do';
		queryProtocolOperation();
	}else if(operationType == 6){
		opeForm.url = 'C_RSC!doInsert.do';
		queryProtocolOperation();
	} 
	
	var rsc_type = $('#rsc_type').val();
	if(rsc_type == 'CISCO' || rsc_type == 'HUAWEI'){
		$('#en_pwd_tr').show();
	}else{
		$('#en_pwd_tr').hide();
	}
	
	if(rsc_type == 'CISCO' || rsc_type == 'IPS' || rsc_type == 'Citrix' || rsc_type == 'HUAWEI' || rsc_type == 'WebSphere' || rsc_type == 'WebLogic'){
		//非应用资源时，隐藏掉 http_url 地段
		$('#tr_http').show();
	}else if(rsc_type == 'Oracle' || rsc_type == 'DB2' || rsc_type == 'MySQL' || rsc_type == 'SQLServer' || rsc_type == 'Sybase'){
		$('#tr_db').show();
		//alert($('#rsc_type').val());
	}else{
		$('#tr_http').hide();
		$('#http_url').attr('disabled', true);
		$('#http_url').val('');
		
		//非数据库时，隐藏掉 tr_db 行
		$('#tr_db').hide();
		$('#sid_service_dbname').attr('disabled', true);
		$('#sid_service_dbname').val('');
		document.getElementById("dbcon_type0").disabled=true;
		document.getElementById("dbcon_type1").disabled=true;
		
	}

	$('#manage_ip').blur(manager_ipChange);
	$('#rsc_type').change(changeRscType);
	$('#is_modify_password').change(changePassword);
	$('#is_modify_en_pwd').change(changeENPassword);
	$('#showPass').change(passwordTypeChange);
	$('#showEditPass').change(editPasswordTypeChange);
	$('#okBtn').click(doOk);
	$('#cancelBtn').click(closeWin);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//选择资源组
function select_c_rsc_grp() {
	var node_id = $('#node_id').val();
	var action = 'C_RSC_GRP!showSelect.do?model.node_id=' + node_id;
	var row = showModalWin(action,'A2');
	
	if (row) {
		$('#rsc_grp_id').val(row.rsc_grp_id);
		$('#rsc_grp_name').val(row.rsc_grp_name);
	}
}

//选中“修改密码”复习框，取消密码输入框禁用状态
function changePassword() {
	var isCheck = $('#is_modify_password').attr('checked');
	var showEditPass = $("#showEditPass").attr("checked");
	if (isCheck) {
		//$('#password').attr('disabled', false);
		if(showEditPass){
			document.getElementById('editPass').innerHTML='<input type="text" id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="" maxlength="50">';
		}else{
			document.getElementById('editPass').innerHTML='<input type="password" id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="" maxlength="50">';
		}
	} else if(!isCheck){
		$('#password').val('');
		$("#showEditPass").attr("checked", false);
		$('#password').attr('disabled', true);
	}
}

//修改时 逻辑判断是否明文显示密码框
function editPasswordTypeChange(){
	var temp = document.getElementById('password').value;
	var isCheck = $('#is_modify_password').attr('checked');
	var showEditPass = $("#showEditPass").attr("checked");
	
	if(showEditPass && isCheck){
		document.getElementById('editPass').innerHTML='<input type="text" id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50">';
	}else if(!showEditPass && isCheck){
		document.getElementById('editPass').innerHTML='<input type=password id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50">';
	} 
	document.getElementById('password').value=temp;
}

//新增时用到
function passwordTypeChange(){
	var temp = document.getElementById('password').value;
	if($("#showPass").attr("checked")){
		document.getElementById('pass').innerHTML='<input type="text" id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50"> &nbsp;';
	}else{
		document.getElementById('pass').innerHTML='<input type=password id="password" name="model.password" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50"> &nbsp;';
	} 
	document.getElementById('password').value=temp;
}


//EN密码，选中“修改密码”复习框，取消密码输入框禁用状态
function changeENPassword() {
	var isCheck = $('#is_modify_en_pwd').attr('checked');
	if (isCheck) {
		$('#en_passwd').val('');
		$('#en_passwd').attr({style:"display:block"});
		$('#en_passwd').attr('disabled', false);
		$('#en_passwd').attr('required', true);
	} else {
		$('#en_passwd').val('');
		$('#en_passwd').attr({style:"display:none"});
		$('#en_passwd').attr('disabled', true);
		$('#en_passwd').attr('required', false);
	}
}

//显示特权账号可编辑状态
function privilegeChange(){
	if($("#showPrivilege").attr("checked")){
		//可编辑文本框
		$('#super_acc').val('');
		$('#super_acc').attr('disabled', true);
		$('#super_acc').attr({style:"display:none"});
		$('#privilege2').attr('disabled', false);
		$('#privilege2').attr({style:"display:block"});
		$('#privilege2').attr('required', true);
	}else{
		//选择列表
		$('#super_acc').attr('disabled', false);
		$('#super_acc').attr({style:"display:block; width:150px"});
		$('#privilege2').val('');
		$('#privilege2').attr('disabled', true);
		$('#privilege2').attr({style:"display:none"});
		$('#privilege2').attr('required', false);
	} 
}

function changeRscType() {
	showOrHideEnPwd();
	queryProtocolOperation();
}

function itemOnChange(){
	$('#rsc_grp_id').val('');
	$('#rsc_grp_name').val('');
}

function showOrHideEnPwd(){
	var rsc_type = $('#rsc_type').val();
	if(rsc_type == 'CISCO' || rsc_type == 'HUAWEI'){
		$('#en_pwd_tr').show();
		$('#en_passwd').attr('disabled', false);
		$('#en_passwd').attr('required', true);
	}else{
		$('#en_passwd').attr('disabled', true);
		$('#en_passwd').attr('required', false);
		$('#en_pwd_tr').hide();
	}
}

function manager_ipChange(){
	var rsc_type = $('#rsc_type').val();
	if (rsc_type == 'WebSphere' || rsc_type == 'WebLogic') {
		$('#http_url').attr('disabled', false);
//		var proItems = $("input[type='checkbox'][name='pro'][checked]").length;
//		//alert('选中的个数：' + proItems);
//		if(proItems>1){
//			showInfoWin('HTTP和HTTPS协议不能同时选中');
//			return;
//		}
		
		$('#resultTable tbody tr').each(function(i,n){ 
			//alert('循环次数：' + i);
			var isCheck = $(this).find('td').eq(0).find('input[type="checkbox"]').attr('checked');
			if (isCheck) {
				var proCode = $(this).find('td').eq(1).find('input[id="protocol"]').val();
				var proName = proCode == '12'?'http':'https';
				var port = $(this).find('td').eq(2).find('input[id="port"]').val();
				var old_url = $('#http_url').val();
				var appName = old_url.substr(old_url.lastIndexOf('/'));
				$('#http_url').val(proName + '://' + $('#manage_ip').val() + ':' + port + '' + appName);
			}
		});
	}else if(rsc_type == 'CISCO' || rsc_type == 'IPS' || rsc_type == 'Citrix' || rsc_type == 'HUAWEI'){
		$('#http_url').attr('disabled', false);
		var count = 0;
		$('#resultTable tbody tr').each(function(i,n){ 
			//alert('循环次数：' + i);
			var isCheck = $(this).find('td').eq(0).find('input[type="checkbox"]').attr('checked');
			if (isCheck) {
				var proCode = $(this).find('td').eq(1).find('input[id="protocol"]').val();
				if(proCode == '12' || proCode == '13'){
					if(count>1){
						showInfoWin('HTTP协议和HTTPS协议只能选中一个！'); 
						return;
					}
					var proName = proCode == '12'?'http':'https';
					var port = $(this).find('td').eq(2).find('input[id="port"]').val();
					var old_url = $('#http_url').val();
					var appName = old_url.substr(old_url.lastIndexOf('/'));
					$('#http_url').val(proName + '://' + $('#manage_ip').val() + ':' + port + '' + appName);
					count++;
				}
			}
		});
	}else{
		return;
	}
} 

//查询操作
function queryProtocolOperation(){
	var rsc_type = $('#rsc_type').val();
	if (isEmpty(rsc_type)) {
		showInfoWin('资源类型不允许为空！');
		return;
	} else if(rsc_type == 'CISCO' || rsc_type == 'IPS' || rsc_type == 'Citrix' || rsc_type == 'HUAWEI' || rsc_type == 'WebSphere' || rsc_type == 'WebLogic'){
		//应用或网络设备时，显示 http_url 地段
		$('#managerAccTD').html('管理账号:&nbsp;&nbsp;');
		$('#tr_http').show();
		$('#http_url').attr('disabled', false);
		$('#sid_service_dbname').attr('disabled', true);
		document.getElementById("dbcon_type0").disabled=true;
		document.getElementById("dbcon_type1").disabled=true;
		//$('#http_url').val('');
		$('#tr_db').hide();
		$('#sid_service_dbname').val('');
	}else if(rsc_type == 'Oracle' || rsc_type == 'DB2' || rsc_type == 'MySQL' || rsc_type == 'SQLServer' || rsc_type == 'Sybase'){
		$('#managerAccTD').html('用户名:&nbsp;&nbsp;');
		$('#http_url').attr('disabled', true);
		$('#tr_http').hide();
		$('#http_url').val('');
		$('#tr_db').show();
		document.getElementById("dbcon_type0").disabled=false;
		document.getElementById("dbcon_type1").disabled=false;
		$('#sid_service_dbname').attr('disabled', false);
		//$('#sid_service_dbname').val('');		
		if(rsc_type == 'Oracle'){
			$('#oracle_db').show();
			$('#db').hide();
		}
		
		if(rsc_type != 'Oracle'){
			$('#oracle_db').hide();
			$('#db').show();
		}		
	}else {
		$('#http_url').attr('disabled', true);
		$('#sid_service_dbname').attr('disabled', true);
		document.getElementById("dbcon_type0").disabled=true;
		document.getElementById("dbcon_type1").disabled=true;
		
		$('#tr_http').hide();
		$('#http_url').val('');
		$('#tr_db').hide();
		$('#sid_service_dbname').val('');
//		var rsc_id = '';
//		if (isNotEmpty($('#rsc_id').val())) {
//			rsc_id = $('#rsc_id').val();
//		}
	}
	
	var rsc_id = '';
	if (isNotEmpty($('#rsc_id').val())) {
		rsc_id = $('#rsc_id').val();
	}
	
	var params = {'model.rsc_id': rsc_id, 'model.rsc_type': rsc_type};
	$.ajax({
		type : "get",
		dataType : "json",
		url : "C_RSC!querySelectedAndNotSelectedProtocolList.do",
		data : params,
		success:function(data){ 	
			clearRowsSMS();
			var rows = data.rows;
			$.each(rows, function(i, n) {
				//var isSelect = (n.IS_SELECTED == '1' ? 'checked': '');
				var str_ssh = 'ssh';
				var str_telnet = 'telnet';
				var defaultSelect;
				if(operationType == '0'){
					if(str_ssh.toUpperCase() == n.PROTOCOL_NAME.toUpperCase() || str_telnet.toUpperCase() == n.PROTOCOL_NAME.toUpperCase()){
						defaultSelect = 'checked';
					}					
					if(rsc_type == 'Oracle' || rsc_type == 'DB2' || rsc_type == 'MySQL' || rsc_type == 'SQLServer' || rsc_type == 'Sybase'){
						//alert(rsc_type);
						defaultSelect = 'checked';
					}else if((rsc_type == 'WebSphere' || rsc_type == 'WebLogic') && 'HTTP'== n.PROTOCOL_NAME.toUpperCase()){
						defaultSelect = 'checked';
						$('#http_url').val(n.PROTOCOL_NAME.toLowerCase() + '://' + $('#manage_ip').val() + ':' + n.PORT + '/');
					}
				}else{
					//alert('e');
					defaultSelect = (n.IS_SELECTED == '1' ? 'checked': '');
				}
				
				var readonly = false;
				if(operationType == '1' && defaultSelect == 'checked'){
					//alert(rsc_type + '----');
					var str = '<tr name="">' +
					'<td><input name="proCheckbox" onClick="return false" type="checkbox" ' +  defaultSelect + '  /></td>' +	
					'<td align="center">' + n.PROTOCOL_NAME + '<input type="hidden" id="protocol" value="' + n.PROTOCOL + '" /></td>' +
					'<td align="center" ><input type="text" id="port" onblur="portChange('+i+')" validType="digit" class="easyui-validatebox  editMainText" value="' + n.PORT + '" with="30%" /></td>' +
				'</tr>';
				}else{
					var str = '<tr>' +
					'<td><input name="pro" type="checkbox" ' +  defaultSelect + '  /></td>' +	
					'<td align="center">' + n.PROTOCOL_NAME + '<input type="hidden" id="protocol" value="' + n.PROTOCOL + '" /></td>' +
					'<td align="center" ><input type="text" id="port" onblur="portChange('+i+')" validType="digit" class="easyui-validatebox  editMainText" value="' + n.PORT + '" with="30%" /></td>' +
				'</tr>';
				}

				$("#resultTable").find('tbody').append(str);
			});
		}   
	});               
}

//端口值改变事件
function portChange(rowIndex){
	
	var rsc_type = $('#rsc_type').val();
	if (isEmpty(rsc_type)) {
		showInfoWin('资源类型不允许为空！');
		return;
	} else if (rsc_type == 'WebSphere' || rsc_type == 'WebLogic' || rsc_type == 'CISCO' || rsc_type == 'IPS' || rsc_type == 'Citrix' || rsc_type == 'HUAWEI') {
		$('#resultTable tbody tr').each(function(i,n){
			var isCheck = $(this).find('td').eq(0).find('input[type="checkbox"]').attr('checked');
			var proCode = $(this).find('td').eq(1).find('input[id="protocol"]').val();
			if (rowIndex == i && isCheck && (proCode == '12' || proCode == '13')) {
				$('#http_url').attr('disabled', false);
				var proCode = $(this).find('td').eq(1).find('input[id="protocol"]').val();
				var port = $(this).find('td').eq(2).find('input[id="port"]').val();
				var proName = proCode == '12'?'http':'https';
				var old_url = $('#http_url').val();
				var appName = old_url.substr(old_url.lastIndexOf('/'));
				$('#http_url').val(proName + '://' + $('#manage_ip').val() + ':' + port + '' + appName);
			}
		});
	}
} 

//清除Table
function clearRowsSMS(){
	var tableLen = document.getElementById('resultTable').rows.length;
	if(tableLen > 1){
		for(var i=0; i<tableLen-1; i++){
			document.getElementById('resultTable').deleteRow(tableLen-i-1);
		}
	}
}

function checkAll(evt){
    $('#resultTable tr').find('input[type="checkbox"]').attr('checked',evt.checked);
}

//保存按钮操作
function doOk() {
	//alert();
	var validateResult = opeForm.form('validate');

	if (!validateResult){
		showInfoWin('请正确填写信息!');
		return;
	}
	
	var isCheck = $('#is_modify_password').attr('checked');
	if (isCheck) {
		var password = $('#password').val();
		if (isEmpty(password)) {
			showInfoWin('你选择修改口令，请输入新口令!');
			return;
		}
	}
	
	var enPwdIsCheck = $('#is_modify_en_pwd').attr('checked');
	if (enPwdIsCheck) {
		var en_passwd = $('#en_passwd').val();
		if (isEmpty(en_passwd)) {
			showInfoWin('你选择修改EN口令，请输入新EN口令!');
			return;
		}
	}
	
	var protocalArr = [];
	var portArr = [];
	
	var defauleSelected = false;

	$('#resultTable tbody tr').each(function(i,n){ 
		var isCheck = $(this).find('td').eq(0).find('input[type="checkbox"]').attr('checked');
		if (isCheck) {
			var protocal = $(this).find('td').eq(1).find('input[id="protocol"]').val();
			
			/**
			 * 普通主机和网络设备必须选中SSH或TELNET协议，数据库必须选中对应类型的协议
			 */
			var rsc_type = $('#rsc_type').val();
			if (isEmpty(rsc_type)) {
				showInfoWin('资源类型不允许为空！');
				return;
			}
			
			if(rsc_type == 'Oracle' || rsc_type == 'DB2' || rsc_type == 'MySQL' || rsc_type == 'SQLServer' || rsc_type == 'Sybase'){
				defauleSelected = true;
			}else if(rsc_type == 'WebSphere' || rsc_type == 'WebLogic'){
				var proItems = $("input[type='checkbox'][name='pro'][checked]").length;
				//alert('数据库' + proItems);
				if(proItems>1){
					//showInfoWin('HTTP和HTTPS协议不能同时选中');
					return;
				}else{
					defauleSelected = true;
				}
			}else if(rsc_type == 'CISCO' || rsc_type == 'IPS' || rsc_type == 'Citrix' || rsc_type == 'HUAWEI'){
				var count = 0;
				//alert('网络设备');
				$('#resultTable tbody tr').each(function(i,n){
					var isCheck = $(this).find('td').eq(0).find('input[type="checkbox"]').attr('checked');
					if (isCheck) {
						var proCode = $(this).find('td').eq(1).find('input[id="protocol"]').val();
						if(proCode == '12' || proCode == '13'){
							if(count>1){
								//showInfoWin('HTTP协议和HTTPS协议只能选中一个！'); 
								return;
							}
							count++;
						}
						defauleSelected = true;
					}
				});
			}else if('0' == protocal.toUpperCase() || '3' == protocal.toUpperCase()){
				defauleSelected = true;
			}
			var port = $(this).find('td').eq(2).find('input[id="port"]').val();
			protocalArr.push(protocal);
			portArr.push(port);
		}
	}); 
	
	if(!defauleSelected){
		showInfoWin('资源协议类型有误！');
		return;
	}

	var protocols = $ .param({'model.pro_id' : protocalArr}, true);
	var ports = $ .param({'model.port' : portArr}, true);
	showSystemProccessing();
	var options = { 
		url: opeForm.url + '?' +  protocols + '&' + ports,
		type: 'post',
		success: function(data){
			removeSystemProccessing();
			getOpenerWin().afterOK();

		    if (operationType == '0') {
				showInfoWin('操作成功!', refreshWin);
			} else {
				showInfoWin('操作成功!', function() {window.close();});
			}
		}
	};

	opeForm.ajaxSubmit(options);
}